﻿using System;
using System.Net;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using TeeGonSdk;
using TeeGonSdk.Domain;
using TeeGonSdk.Request;
using TeeGonSdk.Response;

namespace TeeGon.Net.Sample
{
    public partial class Form1 : Form
    {
        private static ITopClient Client = null;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            Application.Exit();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            txt_client_id.Text = "";
            txt_client_secret.Text = "";
        }

        private bool VerifyIdSecret()
        {
            if (Client != null)
                Client = null;
            txt_result.Text = "";
            if (string.IsNullOrEmpty(txt_client_id.Text) || txt_client_id.Text.Length != 24)
                return false;
            if (string.IsNullOrEmpty(txt_client_secret.Text) || txt_client_secret.Text.Length != 32)
                return false;
            if (!Regex.IsMatch(txt_client_id.Text, @"(^[a-z0-9_]+$)"))
                return false;
            if (!Regex.IsMatch(txt_client_secret.Text, @"(^[a-z0-9_]+$)"))
                return false;
            Client = new DefaultTopClient(lbl_apiurl.Text, txt_client_id.Text, txt_client_secret.Text);
            return true;
        }

        private void btn_account_get_Click(object sender, EventArgs e)
        {
            bool VerifyIdSecretResult = VerifyIdSecret();
            if (!VerifyIdSecretResult)
            {
                txt_result.Text = "请检查client_id和client_secret是否正确填写！";
                return;
            }
            AccountGetRequest get_req = new AccountGetRequest();
            get_req.account_id = "main";
            AccountGetResponse get_rsp = Client.Execute(get_req);
            txt_result.Text = get_rsp.Body;
        }

        private void btn_account_amount_Click(object sender, EventArgs e)
        {
            bool VerifyIdSecretResult = VerifyIdSecret();
            if (!VerifyIdSecretResult)
            {
                txt_result.Text = "请检查client_id和client_secret是否正确填写！";
                return;
            }
            AccountAmountRequest get_req = new AccountAmountRequest();
            get_req.account_id = "main";
            AccountAmountResponse get_rsp = Client.Execute(get_req);
            txt_result.Text = get_rsp.Body;
        }

        private void btn_account_journal_list_Click(object sender, EventArgs e)
        {
            bool VerifyIdSecretResult = VerifyIdSecret();
            if (!VerifyIdSecretResult)
            {
                txt_result.Text = "请检查client_id和client_secret是否正确填写！";
                return;
            }
            AccountJournalListRequest get_req = new AccountJournalListRequest();
            get_req.account_id = "main";
            AccountJournalListResponse get_rsp = Client.Execute(get_req);
            txt_result.Text = get_rsp.Body;
        }

        private void btn_account_status_Click(object sender, EventArgs e)
        {
            bool VerifyIdSecretResult = VerifyIdSecret();
            if (!VerifyIdSecretResult)
            {
                txt_result.Text = "请检查client_id和client_secret是否正确填写！";
                return;
            }
            AccountStatusRequest get_req = new AccountStatusRequest();
            get_req.account_id = "main";
            AccountStatusResponse get_rsp = Client.Execute(get_req);
            txt_result.Text = get_rsp.Body;
        }

        private void btn_charge_Click(object sender, EventArgs e)
        {
            bool VerifyIdSecretResult = VerifyIdSecret();
            if (!VerifyIdSecretResult)
            {
                txt_result.Text = "请检查client_id和client_secret是否正确填写！";
                return;
            }
            ChargeRequest<ChargeActionParams2> get_req = new ChargeRequest<ChargeActionParams2>();
            get_req.amount = 0.01M;
            get_req.out_order_no = Guid.NewGuid().ToString("N").Substring(0, 16);
            get_req.pay_channel = "alipay";
            get_req.ip = "123.123.123.123";
            get_req.subject = "pay:" + DateTime.Now.ToString("HHmmssfff");
            get_req.return_url = "http://api.ha666.com/Teegon/ReturnUrl";
            get_req.notify_url = "http://api.ha666.com/Teegon/NotifyUrl";
            get_req.metadata = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
            get_req.device_id = Dns.GetHostName();
            get_req.charge_type = "pay";
            get_req.account_id = "main";
            ChargeResponse<ChargeActionParams2> get_rsp = Client.Execute(get_req);
            txt_result.Text = get_rsp.Body;
        }

        private void btn_charge_list_Click(object sender, EventArgs e)
        {
            bool VerifyIdSecretResult = VerifyIdSecret();
            if (!VerifyIdSecretResult)
            {
                txt_result.Text = "请检查client_id和client_secret是否正确填写！";
                return;
            }
            ChargeListRequest get_req = new ChargeListRequest();
            get_req.account_id = "main";
            ChargeListResponse get_rsp = Client.Execute(get_req);
            txt_result.Text = get_rsp.Body;
        }

        private void btn_charge_get_Click(object sender, EventArgs e)
        {
            bool VerifyIdSecretResult = VerifyIdSecret();
            if (!VerifyIdSecretResult)
            {
                txt_result.Text = "请检查client_id和client_secret是否正确填写！";
                return;
            }
            ChargeGetRequest get_req = new ChargeGetRequest();
            get_req.id = "4kexkq6ecvqhzz2wdvpcsg62";
            ChargeGetResponse get_rsp = Client.Execute(get_req);
            txt_result.Text = get_rsp.Body;
        }

        private void btn_withdrawal_list_Click(object sender, EventArgs e)
        {
            bool VerifyIdSecretResult = VerifyIdSecret();
            if (!VerifyIdSecretResult)
            {
                txt_result.Text = "请检查client_id和client_secret是否正确填写！";
                return;
            }
            WithdrawalListRequest get_req = new WithdrawalListRequest();
            get_req.account_id = "main";
            WithdrawalListResponse get_rsp = Client.Execute(get_req);
            txt_result.Text = get_rsp.Body;
        }

        private void btn_withdrawal_create_Click(object sender, EventArgs e)
        {
            bool VerifyIdSecretResult = VerifyIdSecret();
            if (!VerifyIdSecretResult)
            {
                txt_result.Text = "请检查client_id和client_secret是否正确填写！";
                return;
            }
            WithdrawalCreateRequest get_req = new WithdrawalCreateRequest();
            get_req.account_id = "main";
            get_req.amount = 0.01M;
            WithdrawalCreateResponse get_rsp = Client.Execute(get_req);
            txt_result.Text = get_rsp.Body;
        }

        private void btn_withdrawal_confirm_Click(object sender, EventArgs e)
        {
            bool VerifyIdSecretResult = VerifyIdSecret();
            if (!VerifyIdSecretResult)
            {
                txt_result.Text = "请检查client_id和client_secret是否正确填写！";
                return;
            }
            WithdrawalConfirmRequest get_req = new WithdrawalConfirmRequest();
            get_req.transaction_no = "pecis5ylk3lhkpntpumw";
            get_req.verfiy_code = 6522904;
            WithdrawalConfirmResponse get_rsp = Client.Execute(get_req);
            txt_result.Text = get_rsp.Body;
        }

        private void btn_bank_url_bind_Click(object sender, EventArgs e)
        {
            bool VerifyIdSecretResult = VerifyIdSecret();
            if (!VerifyIdSecretResult)
            {
                txt_result.Text = "请检查client_id和client_secret是否正确填写！";
                return;
            }
            BankUrlBindRequest get_req = new BankUrlBindRequest();
            get_req.account_id = "main";
            get_req.notify_url = "http://api.ha666.com/Teegon/NotifyUrl";
            get_req.bind_type = "u";
            BankUrlBindResponse get_rsp = Client.Execute(get_req);
            txt_result.Text = get_rsp.Body;
        }
    }
}
